<?php
class colibriPdf extends sfTCPDF {

	public $colors;
	
	public function __construct($orientation = 'P', $unit = 'mm', $format = 'A4', $unicode = true, $encoding = "UTF-8")
	  {
	    parent::__construct($orientation, $unit, $format, $unicode, $encoding);
	    //http://drpeterjones.com/colorcalc/
        
		$this->colors = array(
	        'black' => array(0,0,0),
	        'white' => array(255,255,255),
	        'darkViolet' => array(112,48,160),
	        'lightViolet' => array(166,138,187),
	        'grey' => array(166,166,166),
	        'lightgrey' => array(217,217,217),
			'darkBlue' => array(29, 111, 161),
			'lightBlue' => array(104, 186, 233));
	  }
	
	//Page header
	public function Header() {
		// Logo
		$headderData = $this->getHeaderData();
		$image_file = K_PATH_IMAGES.$headderData['logo'];
		$this->Image($image_file, 5, 5, $headderData['logo_width'], '', 'JPG', '', 'T', false, 300, '', false, false, 0, false, false, false);
		// Set font
		//$this->SetFillColor($this->colors['darkBlue'][0], $this->colors['darckBlue'][1], $this->colors['darckBlue'][2]);
			
		$this->SetFont('helvetica', 'B', 14);
		// Title
		$this->ln();
		$this->Cell(0, 7);
		$this->ln();
		$this->Cell(0, 15, $headderData['title'], 0, true, 'C', 0, '', 0, false, 'M', 'M');
		$this->SetFont('helvetica', 'B', 12);
		$this->Cell(0, 15, $headderData['string'], 0, true, 'C', 0, '', 0, false, 'M', 'M');
		
	}

	// Page footer
	public function Footer() {
		// Position at 15 mm from bottom
		$this->SetY(-10);
		// Set font
		$this->SetFont('helvetica', 'I', 8);
		// Page number
		$this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
	}
	
	// Create a table table
	public function createTable(objectDef $objDef, $items , $hide, $widths, $fontName, $fontSize, $x = null, $title = array()) {
		// Header
		$this->SetDrawColor($this->colors['darkBlue'][0], $this->colors['darkBlue'][1], $this->colors['darkBlue'][2]);
		$this->SetLineWidth(0.2);
		$this->SetFont($fontName, 'B', $fontSize);
		
		$xStart = $this->GetX();
		if(count($title)>0){
			//Calculate total table width
			$i=0;
			foreach($objDef->attributes as $fieldName => $att){
		   		if($att['list'] and !in_array($fieldName, $hide) and $att['type'] != 'List'){
		   			$totalWidth = $totalWidth + $widths[$i];
		   			$i++;
		   		}
	   		}
			$this->Cell($totalWidth, 7,  html_entity_decode($title['text'], ENT_QUOTES, 'UTF-8'), 1, 0, $title['align'], $title['fill']);
			$this->Ln();
		}
		if(!is_null($x)) $this->setX($x);
		// Colors, line width and bold font
		$this->SetFillColor($this->colors['darkBlue'][0], $this->colors['darkBlue'][1], $this->colors['darkBlue'][2]);
		$this->SetTextColor($this->colors['white'][0], $this->colors['white'][1], $this->colors['white'][2]);
		$i=0;
		foreach($objDef->attributes as $fieldName => $att){
	   		if($att['list'] and !in_array($fieldName, $hide) and $att['type'] != 'List'){
	   			$this->Cell($widths[$i], 7,  html_entity_decode($objDef->renderLabel($fieldName), ENT_QUOTES, 'UTF-8'), 1, 0, 'C', 1);
	   			$i++;
	   		}
   		}
		$xFin = $this->GetX();
		$this->Ln();
		if(!is_null($x)) $this->setX($x);
		// Color and font restoration
		$this->SetFillColor($this->colors['white'][0], $this->colors['white'][1], $this->colors['white'][2]);
		$this->SetTextColor($this->colors['black'][0], $this->colors['black'][1], $this->colors['black'][2]);
		$this->SetFont($fontName, '', $fontSize);
		// Data
		$fill = 0;
		foreach($items as $item){
			$i=0;
			foreach($objDef->attributes as $fieldName => $att){
		   		if($att['list'] and !in_array($fieldName, $hide) and $att['type'] != 'List'){
		   			if($att['pattern']=='email'){
		   				$text = html_entity_decode($item[$fieldName], ENT_QUOTES, 'UTF-8');
		   			}else{
		   				$text = html_entity_decode($objDef->renderAttributeValue($item, $fieldName), ENT_QUOTES, 'UTF-8');
		   			}
			   		$this->Cell($widths[$i], 6, $text, 'LR', 0, 'L', $fill, false, 3);
			   		$i++;
		   		}
			}
			$this->Ln();
			if(!is_null($x)) $this->setX($x);
		}
		$this->Line($xStart, $this->GetY(),$xFin, $this->GetY());
		//$this->Cell(array_sum($w), 0, '', 'T');
	}
	
	public function createTableFromArray($columnsConfig, $data, $widths, $fontName, $fontSize, $x = null, $title = array()) {
		$numberFormatter = new sfNumberFormat(sfContext::getInstance()->getUser()->getCulture());
		$this->SetTextColor($this->colors['black'][0], $this->colors['black'][1], $this->colors['black'][2]);
		$this->SetDrawColor($this->colors['darkBlue'][0], $this->colors['darkBlue'][1], $this->colors['darkBlue'][2]);
		$this->SetLineWidth(0.2);
		$this->SetFont($fontName, 'B', $fontSize);
		
		$xStart = $this->GetX();
		if(count($title)>0){
			//Calculate total table width
			$i=0;
			foreach($columnsConfig as $colName => $config){
	   			$totalWidth = $totalWidth + $widths[$i];
	   			$i++;
	   		}
			$this->Cell($totalWidth, 7,  html_entity_decode($title['text'], ENT_QUOTES, 'UTF-8'), 1, 0, $title['align'], $title['fill']);
			$this->Ln();
		}
		if(!is_null($x)) $this->setX($x);
		// Colors, line width and bold font
		// Header
		
		$this->SetTextColor($this->colors['white'][0], $this->colors['white'][1], $this->colors['white'][2]);
		$this->SetFillColor($this->colors['darkBlue'][0], $this->colors['darkBlue'][1], $this->colors['darkBlue'][2]);
		
		$num_headers = count($header);
		$i=0;
		foreach($columnsConfig as $colName => $config){
			//Cell(       $w, 		$h=0, $txt='', 												$border=0, $ln=0, $align='', $fill=false, $link='', $stretch=0, $ignore_min_height=false, $calign='T', $valign='M')
	   		$this->Cell($widths[$i], 7,  html_entity_decode( $config['label'], ENT_QUOTES, 'UTF-8'), 1, 0, 'C', 1);
	   		$i++;
	   		
   		}
		$xFin = $this->GetX();
		$this->Ln();
		$this->SetFont($fontName, '', $fontSize);
		// Data
		
		foreach($data as $row){
			$i=0;
			
			// Color and font restoration
			$this->SetFillColor($this->colors['white'][0], $this->colors['white'][1], $this->colors['white'][2]);
			$this->SetTextColor($this->colors['black'][0], $this->colors['black'][1], $this->colors['black'][2]);
			$fill = 0;
			
			foreach($row as $columnName => $value){
				if(strtolower(substr($value,0,3))=='tot'){
					$this->SetFillColor($this->colors['darkBlue'][0], $this->colors['darkBlue'][1], $this->colors['darkBlue'][2]);
					$this->SetTextColor($this->colors['white'][0], $this->colors['white'][1], $this->colors['white'][2]);
					$fill = 1;
					
				}
				$align = 'L';
				if($columnsConfig[$columnName]['t_function']!=''){
					if($columnsConfig[$columnName]['format']=='' )
					{
						$text = round((float)$value,2);
					}else{
						$text = $numberFormatter->format($value, $columnsConfig[$columnName]['format']);
					}
					$align = 'R';
				}else{
					$text = $value;
				}
				
			   	$this->Cell($widths[$i], 6, html_entity_decode($text, ENT_QUOTES, 'UTF-8'), 'LR', 0, $align, $fill, false, 3);

			   	$i++;
			}
			$this->Ln();
		}
		$this->Line($xStart, $this->GetY(),$xFin, $this->GetY());
		//$this->Cell(array_sum($w), 0, '', 'T');
	}
}